package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserCheckinCommand;
import com.cskaoyan.wordmemorize.service.UserCheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserCheckinController {

    @Autowired
    private UserCheckinService userCheckinService;

    /**
     *
     * 1、当用户复习完当天应该复习的数据就可以签到
     * 2、签到必须只能是对当天签到，一个用户某个日期的签到记录只有一条
     * 3、根据签到统计数据更新用户等级
     * 4、如果用户根据累计签到数可以升到6级，还需要计算用户的当前连续签到数，
     *   如果到升级到6级的同时，其当前连续签到数达到升级要求，可以对用户继续升级。
     *   比如用户在升级到6级的的签到中，最后7次是连续签到，那么用户直接升到7级
     */
    @PostMapping("checkin")
    public Result<UserCheckinStatisticsDTO> checkin(@RequestBody UserCheckinCommand command){
        String id = StpKit.USER.getLoginIdAsString();
        command.setUserId(id);
        UserCheckinStatisticsDTO checkin = userCheckinService.checkin(command);

        return Result.ok(checkin);
    }

    @PutMapping("checkin/card")
    public Result userResignCard() {
        userCheckinService.userResignCard(StpKit.USER.getLoginIdAsLong());
        return Result.ok();
    }

    @GetMapping("checkin/statistics")
    public Result<UserCheckinStatisticsDTO> getCheckInStatistics()
    {

        long loginIdAsLong = StpKit.USER.getLoginIdAsLong();
        UserCheckinStatisticsDTO userCheckinStatistics = userCheckinService.getUserCheckinStatistics(loginIdAsLong);
       return Result.ok(userCheckinStatistics);


    }
    @GetMapping("checkin/list")
    public Result<PageDTO<UserCheckinDTO>> getCheckInList(@ModelAttribute PageRequest pageRequest)
    {
        long loginIdAsLong = StpKit.USER.getLoginIdAsLong();
        if (pageRequest==null)
        {
            return Result.fail();
        }
        PageDTO<UserCheckinDTO> checkInData = userCheckinService.getCheckInData(pageRequest, loginIdAsLong);
        return Result.ok(checkInData);

    }


}
